/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.product.entity.TuangouOrderAction
   Module Description   :

   Date Created      : 2011/8/22
   Original Author   : afra.chou
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.order.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.ec.order.service.OrderService;
import com.yaodian100.ec.shelf.entity.ShelfTuangou;

/**
 * @author afra.chou
 * 
 */
public class TuangouOrderAction extends ActionSupport implements Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8803176601609529327L;

	private String actionResult;
	private SimplePager pager;
	@Resource
	private BaseManager baseMgr;
	@Resource
	private OrderService orderService;

	private String tuangouIdParam;
	private String prodIdParam;
	private String startDtParam;
	private String prodNmParam;
	private List<ShelfTuangou> tuangous;
	private Map<String, String> productNames;

	private String tuangouId;

	private ShelfTuangou tuangou;
	private String productName;

	// tuangouIdParam,prodIdParam,startDtParam,prodNmParam

	public String list() {
		return "list";
	}

	public String query() {
		// CommonCriteria cc = new CommonCriteria();
		// cc.addLe("endDate", new Date());
		// if (StringUtils.isNotBlank(tuangouIdParam)) {
		// cc.addEq("tuangouId", tuangouIdParam);
		// }
		// if (StringUtils.isNotBlank(prodIdParam)) {
		// cc.addEq("productId", prodIdParam);
		// }
		// if (StringUtils.isNotBlank(startDtParam)) {
		// cc.addEq("startDate", startDtParam);
		// }
		// if (StringUtils.isNotBlank(prodNmParam)) {
		// // cc.
		// }

		StringBuffer hql = new StringBuffer(
				"select sh, p.productId, p.productName from ShelfTuangou sh, Product p where p.productId=sh.productId ");
		List paramList = new ArrayList();
		if (StringUtils.isNotBlank(prodNmParam)) {
			hql.append(" and sh.productId like ?");
			paramList.add("%" + prodIdParam + "%");
		}
		if (StringUtils.isNotBlank(startDtParam)) {
			hql.append(" and to_char(sh.startDate,'yyyy-mm-dd') = ?");
			paramList.add(startDtParam);
		}
		hql.append(" and sh.endDate < current_date");
		if (StringUtils.isNotBlank(prodIdParam)) {
			hql.append(" and sh.productId = ?");
			paramList.add(prodIdParam);
		}
		hql.append(" and sh.tuangouStatus = '0'");
		if (StringUtils.isNotBlank(tuangouIdParam)) {
			hql.append(" and sh.tuangouId = ?");
			paramList.add(tuangouIdParam);
		}

		getPager().setTotalSize(baseMgr.countByHql(hql.toString(), paramList.toArray()));

		List queryResult = new ArrayList(Arrays.asList(baseMgr.queryByHql(hql.toString(), paramList.toArray(), getPager()
				.getCurrentPage()
				* getPager().getPageRecord(), getPager().getPageRecord())));
		tuangous = new ArrayList<ShelfTuangou>();
		productNames = new HashMap<String, String>();
		for (Iterator<Object[]> it = queryResult.iterator(); it.hasNext();) {
			Object[] thisRow = it.next();
			tuangous.add((ShelfTuangou) thisRow[0]);
			productNames.put((String) thisRow[1], (String) thisRow[2]);

		}

		// tuangous = new ArrayList(Arrays.asList(baseMgr.queryByHql(hql.toString(), paramList.toArray(), getPager()
		// .getCurrentPage()
		// * getPager().getPageRecord(), getPager().getPageRecord())));

		return "list";
	}

	public String tuangouFail() {
		if (StringUtils.isNotBlank(tuangouId)) {
			orderService.processFailTuangouOrder(tuangouId);
			actionResult = "true";
		}
		return "cancelOrders";
	}

	public String tuangouSuccess() {
		if (StringUtils.isNotBlank(tuangouId)) {
			orderService.processSuccessTuangouOrder(tuangouId);
			actionResult = "true";
		}

		return "syncOrders";
	}

	public String getTuangouInfo() {
		StringBuffer hql = new StringBuffer(
				"select sh, p.productName from ShelfTuangou sh, Product p where p.productId=sh.productId and sh.tuangouId = ? order by sh.tuangouId");
		List paramList = new ArrayList();
		paramList.add(tuangouId);

		List queryResult = new ArrayList(Arrays.asList(baseMgr.queryByHql(hql.toString(), paramList.toArray())));
		if (queryResult.size() > 0) {
			Object[] thisRow = (Object[]) queryResult.get(0);
			tuangou = (ShelfTuangou) thisRow[0];
			productName = (String) thisRow[1];
		}

		return "viewTuanguoInfo";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		getPager();

	}

	/**
	 * @return the actionResult
	 */
	public String getActionResult() {
		return actionResult;
	}

	/**
	 * @param actionResult the actionResult to set
	 */
	public void setActionResult(String actionResult) {
		this.actionResult = actionResult;
	}

	/**
	 * @return the pager
	 */
	public SimplePager getPager() {
		if (pager == null) {
			pager = new SimplePager();
		}
		return pager;
	}

	/**
	 * @param pager the pager to set
	 */
	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	/**
	 * @return the tuangouIdParam
	 */
	public String getTuangouIdParam() {
		return tuangouIdParam;
	}

	/**
	 * @param tuangouIdParam the tuangouIdParam to set
	 */
	public void setTuangouIdParam(String tuangouIdParam) {
		this.tuangouIdParam = tuangouIdParam;
	}

	/**
	 * @return the prodIdParam
	 */
	public String getProdIdParam() {
		return prodIdParam;
	}

	/**
	 * @param prodIdParam the prodIdParam to set
	 */
	public void setProdIdParam(String prodIdParam) {
		this.prodIdParam = prodIdParam;
	}

	/**
	 * @return the startDtParam
	 */
	public String getStartDtParam() {
		return startDtParam;
	}

	/**
	 * @param startDtParam the startDtParam to set
	 */
	public void setStartDtParam(String startDtParam) {
		this.startDtParam = startDtParam;
	}

	/**
	 * @return the prodNmParam
	 */
	public String getProdNmParam() {
		return prodNmParam;
	}

	/**
	 * @param prodNmParam the prodNmParam to set
	 */
	public void setProdNmParam(String prodNmParam) {
		this.prodNmParam = prodNmParam;
	}

	/**
	 * @return the tuangous
	 */
	public List<ShelfTuangou> getTuangous() {
		return tuangous;
	}

	/**
	 * @param tuangous the tuangous to set
	 */
	public void setTuangous(List<ShelfTuangou> tuangous) {
		this.tuangous = tuangous;
	}

	/**
	 * @return the productNames
	 */
	public Map<String, String> getProductNames() {
		return productNames;
	}

	/**
	 * @param productNames the productNames to set
	 */
	public void setProductNames(Map<String, String> productNames) {
		this.productNames = productNames;
	}

	/**
	 * @return the tuangouId
	 */
	public String getTuangouId() {
		return tuangouId;
	}

	/**
	 * @param tuangouId the tuangouId to set
	 */
	public void setTuangouId(String tuangouId) {
		this.tuangouId = tuangouId;
	}

	/**
	 * @return the tuangou
	 */
	public ShelfTuangou getTuangou() {
		return tuangou;
	}

	/**
	 * @param tuangou the tuangou to set
	 */
	public void setTuangou(ShelfTuangou tuangou) {
		this.tuangou = tuangou;
	}

	/**
	 * @return the productName
	 */
	public String getProductName() {
		return productName;
	}

	/**
	 * @param productName the productName to set
	 */
	public void setProductName(String productName) {
		this.productName = productName;
	}

}
